<?php namespace app\service\controller;
use think\Controller;
use think\Db;

/**
 * 职位关联服务层
 */
class AuthGroupAccess extends Controller{
	
	private $modelObj;				//模型句柄
	public function __construct(){
		parent::__construct();
		if(empty($this -> modelObj)) $this -> modelObj = new \app\model\model\AuthGroupAccess;
	}
	
	/**
	 * 关联
	 * med_auth_group表
	 * 获取多个用户职位
	 */
	public function getJoinAuthgroupMuchData($in){
		if( empty($in) ){
			$outArr['code'] = '40004';
		}else{

			$where['a.uid'] = isset($in['a.userId']) ? $in['a.userId'] : '';				//
			$whereExp = [];
			foreach ($in as $key => $value) {
				if( gettype($key) == 'integer' && in_array($value[1], ['in', 'not in', 'like', 'between', 'eq', 'neq', 'lt', 'lt']) && count($value) == 3 ) $whereExp[] = [$value[0], $value[1], $value[2]];
			}
			
			$orderBy = isset($in['orderBy']) ? $in['orderBy'] : '';								//排序
			$groupBy = isset($in['groupBy']) ? $in['groupBy'] : '';								//分组
			$join = isset($in['join']) ? $in['join'] : 'left';									//join
			
			$field = isset($in['field']) ? $in['field'] : '';									//要获取的字段
			
			$where = filterArr($where, array_keys($where), []);
			
			if(empty($where) && empty($whereExp)){
				$outArr['code'] = '40004';
			}else{
				$data = $this -> modelObj -> getJoinAuthgroupMuchInfo($where, $whereExp, $field, $orderBy, $groupBy, $join);
				
				if(!$data){
					$outArr['code'] = '99999';
				}else{
					
					if( isset($in['isGetRule']) && $in['isGetRule'] == 1 ){
//						获取职位权限
						$ruleWhere = [];
						$rulesId = [];
						foreach ($data as $k => $v) {
							if( $v['rules'] != '*' ) $rulesId =  array_merge($rulesId, explode(',', $v['rules']));
							if( $v['rules'] == '*' ) {
								$rulesId = [];
								break;
							}
						}
						if( !empty($rulesId) ) $ruleWhere[] = ['id', 'in', $rulesId];
						$ruleWhere[] = ['is_delete', 'eq', '0'];
						$ruleList = model("model/AuthRule") -> getMuchInfo($ruleWhere, ['name', 'title', 'ismenu', 'path'], 'id asc');
						$menuList = [];
						$operation = [];
						if( !empty($ruleList) ){
							foreach ($ruleList as $k => $v) {
								if( $v['ismenu'] == 1 ) $menuList[] = $v['path'];
								if( $v['ismenu'] != 1 ) $operation[] = $v['path'];
							}
						}
						$data['menuList'] = $menuList;
						$data['operation'] = $operation;
					}
					
					$outArr['code'] = '1';
					$outArr['info'] = $data;
				}
			}
		}
		return $outArr;
	}
	
	/**
	 * 获取多条数据
	 */
//	public function getMuchData($in){
//		if( empty($in) ){
//			$outArr['code'] = '40004';
//		}else{
//
//			$where['status'] = isset($in['status']) ? trim($in['status']) : '';					//状态
//			$where['district_id'] = isset($in['districtId']) ? $in['districtId'] : '';			//所属服务站区域ID
////			$where['place'] = isset($in['place']) ? $in['place'] : '';							//是否允许发布内容
//			$where['Ismobile'] = isset($in['ismobile']) ? $in['ismobile'] : '';					//是否为移动端
//			
//			$orderBy = isset($in['orderBy']) ? $in['orderBy'] : '';								//排序
//			$groupBy = isset($in['groupBy']) ? $in['groupBy'] : '';								//分组
//			$page = isset($in['page']) ? $in['page'] : '';										//页码
//			$num = isset($in['num']) ? $in['num'] : '';											//数量
//			
//			$field = isset($in['field']) ? $in['field'] : '';									//要获取的字段
//			
//			$where = filterArr($where, array_keys($where), []);
//			
//			if(empty($where)){
//				$outArr['code'] = '40004';
//			}else{
//				$data = $this -> modelObj -> getMuchInfo($where, $field, $orderBy, $groupBy, $page, $num);
//				
//				if(!$data){
//					$outArr['code'] = '99999';
//				}else{
//					$outArr['code'] = '1';
//					$outArr['info'] = $data;
//				}
//			}
//		}
//		return $outArr;
//	}
	
	/**
	 * 获取指定数据
	 */
//	public function getSingleData($in){
//		if(empty($in)){
//			$out_arr['code'] = '40004';
//		}else{
//
//			$where['username'] = isset($in['userName']) ? trim($in['userName']) : '';				//
//			$field = isset($in['field']) ? $in['field'] : '';										//要获取的字段
//			
//			$where = filterArr($where, array_keys($where), []);
//			
//			$data = $this -> modelObj -> getSingleInfo($where, $field);
//			if(!$data){
//				$outArr['code'] = '99999';
//			}else{
//				$outArr['code'] = '1';
//				$outArr['info'] = $data;
//			}
//		}
//		return $outArr;
//	}
	
}
